﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Skybot.Net.WebSockets.Sockets
{
    /// <summary>
    /// 用于WebScoet http握手头
    /// </summary>
    /// <remarks>
    ///GET / HTTP/1.1
    ///Origin: null
    ///Sec-WebSocket-Key: oKfFdr/CpYsowpcCXpPeKg==
    ///Connection: Upgrade
    ///Upgrade: Websocket
    ///Sec-WebSocket-Version: 13
    ///User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
    ///Host: localhost:8181
    ///Cache-Control: no-cache
    /// </remarks>
    public class WebSocketHttpRequestHead
    {



        public WebSocketHttpRequestHead() { }

        /// <summary>
        /// 创建一个实例对像
        /// </summary>
        public WebSocketHttpRequestHead(string httpRequestString)
        {
            //开始填充数据
            var arr = httpRequestString.Split(splictString, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in arr)
            {
                int indexof = item.IndexOf(":");
                if (indexof != -1)
                {

                    string key = item.Substring(0, indexof);
                    string val = item.Substring(indexof + 1).Trim();  
                    switch (key)
                    {
                        //Origin: null
                        case "Origin":
                            Origin = val;
                            _headers.Add(HeadEnum.Origin, val);
                            break;
                        //Sec-WebSocket-Key: oKfFdr/CpYsowpcCXpPeKg==
                        case "Sec-WebSocket-Key":
                            Sec_WebSocket_Key = val;
                            _headers.Add(HeadEnum.Sec_WebSocket_Key, val);

                            break;
                        //Connection: Upgrade
                        case "Connection":
                            Connection = val;
                            _headers.Add(HeadEnum.Connection, val);

                            break;
                        //Upgrade: Websocket
                        case "Upgrade":
                            Upgrade = val;
                            _headers.Add(HeadEnum.Upgrade, val);

                            break;
                        //Sec-WebSocket-Version: 13
                        case "Sec-WebSocket-Version":
                            Sec_WebSocket_Version = val;
                            _headers.Add(HeadEnum.Sec_WebSocket_Version, val);

                            break;
                        //User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
                        case "User-Agent":
                            User_Agent = val;
                            _headers.Add(HeadEnum.User_Agent, val);

                            break;
                        //Host: localhost:8181
                        case "Host":
                            Host = val;
                            _headers.Add(HeadEnum.Host, val);

                            break;
                        //Cache-Control: no-cache
                        case "Cache-Control":
                            Cache_Control = val;
                            _headers.Add(HeadEnum.Cache_Control, val);

                            break;

                    }

                }
            }
        }

        protected static string[] splictString = new string[] { "\r\n" };
        private readonly IDictionary<HeadEnum, string> _headers = new Dictionary<HeadEnum, string>(20);

        public string Origin { get; set; }

        public string Sec_WebSocket_Key { get; set; }

        public string Connection { get; set; }

        public string Upgrade { get; set; }

        public string Sec_WebSocket_Version { get; set; }
        public string User_Agent { get; set; }

        public string Host { get; set; }

        public string Cache_Control { get; set; }

        public string this[HeadEnum name]
        {
            get
            {
                string value;
                return _headers.TryGetValue(name, out value) ? value : default(string);
            }
        }



    }

    /// <summary>
    /// http头
    /// </summary>
    public enum HeadEnum
    {
        Origin,
        Sec_WebSocket_Key,
        Connection,
        Upgrade,
        Sec_WebSocket_Version,
        User_Agent,
        Host,
        Cache_Control,
    }
}
